package java大项目.a1学生管理;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {
    private static final String ADD_STUDENT="1";
    private static final String DELETESTUDENT="2";
    private static final String UPDATESTUDENT="3";
    private static final String QUERYSTUDENT="4";
    private static final String EXIT="5";

    public static void startStudentSystem() {
        //创建一个集合
        ArrayList<Student> list=new ArrayList<>();
        //菜单：
        loop:while (true) {
            System.out.println("------------欢迎来到小u学习管理系统------------");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择");
            Scanner sc=new Scanner(System.in);
            String choose = sc.next();
            switch (choose){
                case ADD_STUDENT:
                    addStundet(list);
                    break;

                case DELETESTUDENT:
                    deleteStudent(list);
                    break;
                case UPDATESTUDENT:
                    updateStudent(list);
                    break;
                case QUERYSTUDENT:
                    queryStudent(list);
                    break;
                case EXIT:
                    System.out.println("程序已退出");
                    break loop;
                default:
                    System.out.println("没有这个选项");
                    break;
            }
        }
    }


    //添加学生
    public static void addStundet(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        //创建学生对象
        Student s=new Student();

        while (true) {
            System.out.println("请输入学生的id");
            String id = sc.next();
            boolean flag = contains(list, id);
            if (flag){
                System.out.println("id已经存在，请重新录入");
            }else {
                s.setId(id);
                break;
            }
        }


        System.out.println("请输入学生的姓名");
        String name = sc.next();
        s.setName(name);

        System.out.println("请输入学生的年龄");
        int age = sc.nextInt();
        s.setAge(age);

        System.out.println("请输入学生的家庭住址");
        String address = sc.next();
        s.setAddress(address);

        //把学生对象添加到学生当做
        list.add(s);
        //提示一下用户
        System.out.println("学生信息添加成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要删除的id");
        String id = sc.next();
        int index = getIndex(list, id);
        //对index判断
        if (index>=0){
            //直接删除
            list.remove(index);
            System.out.println("id为：" +id+"的学生删除成功");
        }else {
            System.out.println("id不存在，删除失败");
        }

    }
    //修改学生
    public static void updateStudent(ArrayList<Student> list){
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要修改学生的id");
        String id = sc.next();

        int index = getIndex(list, id);

        if (index==-1){
            System.out.println("要修改的id:"+id+"不存在");
            return;
        }
        //当代码执行到这里，表示id存在
        Student stu = list.get(index);//要修改的信息

        System.out.println("请输入要修改的学生姓名");
        String newName = sc.next();
        stu.setName(newName);

        System.out.println("请输入要修改的学生年龄");
        int newAge = sc.nextInt();
        stu.setAge(newAge);

        System.out.println("请输入要修改的学生家庭住址");
        String newAddress = sc.next();
        stu.setAddress(newAddress);

        System.out.println("学生信息修改成功");
    }
    //查询学生
    public static void queryStudent(ArrayList<Student> list){
        if (list.size()==0){
            System.out.println("当前无学生信息，请添加后再查询");
            return;//结束这个方法
        }
        //当代码执行到这里。说明集合是有信息的。
        //先打印表头信息
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());

        }
    }
    //判断id在集合中是否存在
    public static boolean contains(ArrayList<Student> list,String id){
        return getIndex(list,id)>=0;
    }
    //通过id判断获取索引
    public static int getIndex(ArrayList<Student> list,String id){
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            String sid = stu.getId();
            if (sid.equals(id)){
                return i;
            }
        }
        return -1;
    }
}


